﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Searching.Data;

namespace Searching.UI
{
    public partial class StoreForm : Form
    {
        private PageStore store = PageStore.GetInstance();
        private WebPage[] pages = null;

        public StoreForm(WebPage[] pages)
        {
            InitializeComponent();

            this.pages = pages;
            this.Shown += new EventHandler(StoreForm_Shown);
//            this.FormClosing += new FormClosingEventHandler(StoreForm_FormClosing);

            if (pages != null)
            {
                this.Text = "Result";
                btnSave.Visible = false;
            }
        }

        void StoreForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult result = MessageBox.Show("是否保存修改", "关闭", MessageBoxButtons.YesNoCancel);
            if (result == DialogResult.Yes)
            {
                MessageBox.Show("Yes");
            }
            else if (result == DialogResult.No)
            {
                MessageBox.Show("No");
            }
            else if (result == DialogResult.Cancel)
            {
                MessageBox.Show("Cancel");
              
            }
        }

        void StoreForm_Shown(object sender, EventArgs e)
        {
            SetDataGrid();
        }

        public void SetDataGrid()
        {
            dgPages.Rows.Clear();

            if (pages == null)
            {
                pages = store.GetAllPages();
            }
            for (int i = 0; i < pages.Length; ++i)
            {
                object[] cells = new object[6]{(i+1).ToString(), "", "", "", "", ""};
                string[] words = pages[i].GetKeywords();
                cells[0] = pages[i].GetID();
                for (int j = 0; j < words.Length; ++j )
                {
                    cells[j + 1] = words[j];
                }
                dgPages.Rows.Add(cells);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            DataGridViewRowCollection rows = dgPages.Rows;
            if (rows.Count > 31)
            {
                MessageBox.Show("网页数量过大，不能超过30条");
                return;
            }

            store.DeleteAll();

            for (int i = 0; i < rows.Count - 1; ++i)
            {
                DataGridViewCellCollection cells = rows[i].Cells;
                if (cells[0].Value == null)
                {
                    continue;
                }

                string[] words = new string[5] { "", "", "", "", "" };
                for (int j = 1; j < cells.Count; ++j)   //第0个为编号
                {
                    if (cells[j].Value != null)
                    {
                        words[j-1] = cells[j].Value.ToString();
                    }
                }
                store.AddPage(new WebPage(cells[0].Value.ToString(), words));
            }
            MessageBox.Show("Save Success");
            this.Close();
        }
    }
}
